<?php
/**
 * Created by PhpStorm.
 * User: LFL
 * Date: 2010/5/25
 * Time: 16:02
 */
namespace api\controllers;

use Yii;
use yii\web\Controller;

class MaterielController extends Controller
{
    //查询商品
    public function actionGetMateriel()
    {
        $data = Yii::$app->request->post();

        $sql = '';
//        $sql_list = 'SELECT ma.*,wh.warehouseName,ma.id as materielId FROM j_materiel ma
//                INNER JOIN j_materiel_category c2 ON ma.midCategory=c2.id AND c2.level=2
//                INNER JOIN j_materiel_category c3 ON ma.smallCategory=c3.id AND c3.level=3
//                LEFT JOIN j_warehouse wh ON ma.warehouseId=wh.id WHERE ma.useFlg=1 AND ma.saleFlg=1 ';
        $sql_list = 'SELECT ma.*,wh.warehouseName,ma.id as materielId FROM j_materiel ma
                LEFT JOIN j_warehouse wh ON ma.warehouseId=wh.id WHERE ma.useFlg=1 AND ma.saleFlg=1 ';

        if (isset($data['searchKey']) && !empty($data['searchKey'])) {
            $sql .= ' AND (ma.materielName like "%' . $data['searchKey'] . '%"  OR ma.materielCode like "%' . $data['searchKey'] . '%")';
        }

//        if (isset($data['bigCategory']) && !empty($data['bigCategory'])) {
//            $sql .= ' AND ma.bigCategory=' . $data['bigCategory'];
//        }
//
//        if (isset($data['midCategory']) && !empty($data['midCategory'])) {
//            $sql .= ' AND ma.midCategory=' . $data['midCategory'];
//        }
//
//        if (isset($data['smallCategory']) && !empty($data['smallCategory'])) {
//            $sql .= ' AND ma.smallCategory=' . $data['smallCategory'];
//        }
//
        if (isset($data['shopId']) && !empty($data['shopId'])) {
            $sql .= " AND ( ma.shopGroupId is null OR ma.shopGroupId = '' OR fun_ck_shop_new((SELECT GROUP_CONCAT(j_shop_group.shopIds) from j_shop_group where FIND_IN_SET(j_shop_group.id,ma.shopGroupId)), '" . $data['shopId'] . "')) ";
        }

        if (isset($data['warehouseId']) && !empty($data['warehouseId'])) {
            $sql .= ' AND ma.warehouseId=' . $data['warehouseId'];
        }

        if (isset($data['sortName']) && !empty($data['sortName'])) {
            $sql .= ' ORDER BY ' . $data['sortName'];
        }
        if (isset($data['sortOrder']) && !empty($data['sortOrder'])) {
            $sql .= ' ' . $data['sortName'];
        }
        $page = (isset($data['page']) && !empty($data['page'])) ? $data['page'] : 0;
        $length = (isset($data['length']) && !empty($data['length'])) ? $data['length'] : 20;

        //$sql .= ' ORDER BY c2.sort,c3.sort,ma.sort DESC, ma.id';
        $sql .= ' ORDER BY ma.sort DESC, ma.id';
        $sql .= ' LIMIT ' . ($page*$length) . ',' . $length;
        Yii::error($sql_list . $sql);
        $list = Yii::$app->db->createCommand($sql_list . $sql)->queryAll();
        if ($list) {
            foreach ($list as &$ma) {
                $amount = 0;
                if (isset($data['userId']) && !empty($data['userId'])) {
                    $cartSql = "SELECT amount FROM j_cart WHERE userId='" . $data['userId'] . "' AND materielId = '" . $ma['materielId'] . "'";
                    $cart = Yii::$app->db->createCommand($cartSql)->queryOne();
                    $amount = $cart ? $cart['amount'] : 0;
                }
                $img = $ma['img'] ? $ma['img'] : 'materiel/default.jpg';
                $ma['amount'] = $amount;
                $ma['img'] = Yii::$app->params['WEB_IMG_URL'] . $img;
            }
        }

        $this->retJson('', 200, $list);
    }

    //添加购物车
    public function actionAddCart()
    {
        $data = Yii::$app->request->post();
        if (!isset($data['userId']) || empty($data['userId']) || !isset($data['list']) || !is_array($data['list'])) {
            $this->retJson('参数错误', 500);
        }

        $sql = '';
        foreach ($data['list'] as $ma) {
            $amount = isset($ma['amount']) ? $ma['amount'] : 1;
            //删除
            if ($amount == 0) {
                $sql .= "DELETE FROM j_cart WHERE userId = '" . $data['userId'] . "' AND materielId='" . $ma['materielId'] . "';";
            }
            $cksql = "SELECT COUNT(0) CNT FROM j_cart WHERE userId = '" . $data['userId'] . "' AND materielId='" . $ma['materielId'] . "';";
            $ckRes = Yii::$app->db->createCommand($cksql)->queryOne();
            if ($ckRes && $ckRes['CNT']) {

                if (isset($ma['editFlg'])) {
                    $sql .= "UPDATE j_cart SET amount = " . $amount . " WHERE userId = '" . $data['userId'] . "' AND materielId='" . $ma['materielId'] . "';";
                } else {
                    $sql .= "UPDATE j_cart SET amount = amount+" . $amount . " WHERE userId = '" . $data['userId'] . "' AND materielId='" . $ma['materielId'] . "';";
                }

            } else {
                $cart = array(
                    'creator' => $data['userId'],
                    'createTime' => date('Y-m-d H:i:s'),
                    'userId' => $data['userId'],
                    'materielId' => $ma['materielId'],
                    'amount' => $amount,
                );
                Yii::$app->db->createCommand()->insert('j_cart', $cart)->execute();
            }
        }
        if ($sql) Yii::$app->db->createCommand($sql)->execute();

        $this->retJson('ok');
    }

    //购物车查询
    public function actionGetCart()
    {
        $data = Yii::$app->request->post();
        if (!isset($data['userId']) || empty($data['userId'])) {
            $this->retJson('参数错误', 500);
        }
        $sql = "SELECT ma.materielName,ma.salePrice,ma.specDesc,ma.packDesc,ma.img,ma.orderUnit,ca.amount,ca.materielId,wh.id as warehouseId, wh.warehouseName
                FROM j_cart ca INNER JOIN j_materiel ma ON ca.materielId=ma.id
                INNER JOIN j_warehouse wh ON ma.warehouseId=wh.id
                INNER JOIN j_materiel_category c1 ON ma.bigCategory=c1.id AND c1.level=1
                INNER JOIN j_materiel_category c2 ON ma.midCategory=c2.id AND c2.level=2
                INNER JOIN j_materiel_category c3 ON ma.smallCategory=c3.id AND c3.level=3
                WHERE ma.useFlg=1 AND ma.saleFlg=1 AND ma.startTime <= NOW() AND ma.endTime >= NOW() AND ca.userId= '" . $data['userId'] . "'";

        //判断shopid
        if (isset($data['shopId']) && !empty($data['shopId'])) {
            $sql .= " AND ( ma.shopGroupId is null OR ma.shopGroupId = '' OR fun_ck_shop_new((SELECT GROUP_CONCAT(j_shop_group.shopIds) from j_shop_group where FIND_IN_SET(j_shop_group.id,ma.shopGroupId)), '" . $data['shopId'] . "')) ";
        }

        $sql .= 'ORDER BY ma.warehouseId,c1.sort,c2.sort,c3.sort,ma.id';
        $result = Yii::$app->db->createCommand($sql)->queryAll();
        if ($result) {
            foreach ($result as &$ma) {
                $img = $ma['img'] ? $ma['img'] : 'materiel/default.jpg';
                $ma['img'] = Yii::$app->params['WEB_IMG_URL'] . $img;
            }
        }
        $this->retJson('ok', 200, $result);
    }
}